package com.aimee.table.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aimee.table.R;
import com.aimee.table.bean.Fruit;
import com.aimee.table.util.DpTool;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FruitAdapter extends BaseAdapter {

    private Context context;
    private List<Fruit> fruitList = new ArrayList<>();

    public FruitAdapter(Context context, List<Fruit> fruitList) {
        this.context = context;
        this.fruitList = fruitList;
    }

    @Override
    public int getCount() {
        return fruitList == null ? 0 : fruitList.size();
    }

    @Override
    public Object getItem(int position) {
        return fruitList == null ? null : fruitList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_fruit, null);
            holder = new ViewHolder(view);
            holder.llFruitItem.setDividerDrawable(context.getResources().getDrawable(R.drawable.divider));
            holder.llFruitItem.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.tvEvaluate.setVisibility(position == 0 ? View.VISIBLE : View.GONE);
        holder.btnSweet.setVisibility(position != 0 ? View.VISIBLE : View.GONE);
        holder.btnOrdinary.setVisibility(position != 0 ? View.VISIBLE : View.GONE);
        holder.btnSour.setVisibility(position != 0 ? View.VISIBLE : View.GONE);
        if (position == 0) {
            holder.tvName.setText(R.string.str_name);
            holder.tvNumber.setText(R.string.str_number);
            holder.tvPrice.setText(R.string.str_price);
            holder.tvEvaluate.setText(R.string.str_evaluate);
        } else {
            Fruit fruit = fruitList.get(position);
            if (fruit != null) {
                holder.tvName.setText(fruit.getName());
                holder.tvNumber.setText(fruit.getNum());
                holder.tvPrice.setText(fruit.getPrice());
            }

        }
        return view;
    }

    static
    class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_number)
        TextView tvNumber;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_evaluate)
        TextView tvEvaluate;
        @BindView(R.id.btn_sweet)
        Button btnSweet;
        @BindView(R.id.btn_ordinary)
        Button btnOrdinary;
        @BindView(R.id.btn_sour)
        Button btnSour;
        @BindView(R.id.ll_fruit_item)
        LinearLayout llFruitItem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
